use std::env;
-use std::fs::{self, File};
+use std::fs;
use std::io::prelude::*;
-use std::io;
use std::path::Path;
use rustc_serialize::{Decodable, Decoder};
use term::color::BLACK;
use util::{GitRepo, HgRepo, CargoResult, human, ChainError, internal};
-use util::Config;
+use util::{Config, paths};
use toml;
GitRepo::discover(path).is_ok() || HgRepo::discover(path).is_ok()
}
-fn file(p: &Path, contents: &[u8]) -> io::Result<()> {
- try!(File::create(p)).write_all(contents)
-}
-
fn mk(config: &Config, path: &Path, name: &str,
opts: &NewOptions) -> CargoResult<()> {
let cfg = try!(global_config(config));
match vcs {
VersionControl::Git => {
try!(GitRepo::init(path));
- try!(file(&path.join(".gitignore"), ignore.as_bytes()));
+ try!(paths::write(&path.join(".gitignore"), ignore.as_bytes()));
},
VersionControl::Hg => {
try!(HgRepo::init(path));
- try!(file(&path.join(".hgignore"), ignore.as_bytes()));
+ try!(paths::write(&path.join(".hgignore"), ignore.as_bytes()));
},
VersionControl::NoVcs => {
try!(fs::create_dir(path));
(None, None, name, None) => name,
};
- try!(file(&path.join("Cargo.toml"), format!(
+ try!(paths::write(&path.join("Cargo.toml"), format!(
r#"[package]
name = "{}"
version = "0.1.0"
try!(fs::create_dir(&path.join("src")));
if opts.bin {
- try!(file(&path.join("src/main.rs"), b"\
+ try!(paths::write(&path.join("src/main.rs"), b"\
fn main() {
println!(\"Hello, world!\");
}
"));
} else {
- try!(file(&path.join("src/lib.rs"), b"\
+ try!(paths::write(&path.join("src/lib.rs"), b"\
#[test]
fn it_works() {
}
use std::collections::HashMap;
-use std::fs::{self, File};
+use std::fs;
use std::io::prelude::*;
use std::path::PathBuf;
use std::str;
use core::{PackageId, PackageSet};
use util::{CargoResult, human, Human};
-use util::{internal, ChainError, profile};
+use util::{internal, ChainError, profile, paths};
use util::Freshness;
use super::job::Work;
let parsed_output = try!(BuildOutput::parse(output, &pkg_name));
build_state.insert(id, kind, parsed_output);
- try!(File::create(&build_output.parent().unwrap().join("output"))
- .and_then(|mut f| f.write_all(output.as_bytes()))
- .map_err(|e| {
- human(format!("failed to write output of custom build command: {}",
- e))
- }));
-
+ try!(paths::write(&build_output.parent().unwrap().join("output"),
+ output.as_bytes()));
Ok(())
});
let dirty = work.then(dirty);
let fresh = Work::new(move |_tx| {
let (id, pkg_name, build_state, build_output) = all;
- let new_loc = build_output.parent().unwrap().join("output");
- let mut f = try!(File::open(&new_loc).map_err(|e| {
- human(format!("failed to read cached build command output: {}", e))
- }));
- let mut contents = String::new();
- try!(f.read_to_string(&mut contents));
+ let contents = try!(paths::read(&build_output.parent().unwrap()
+ .join("output")));
let output = try!(BuildOutput::parse(&contents, &pkg_name));
build_state.insert(id, kind, output);
Ok(())
use toml::{self, Encoder, Value};
use core::{Resolve, resolver, Package, SourceId};
-use util::{CargoResult, ChainError, human};
+use util::{CargoResult, ChainError, human, paths};
use util::toml as cargo_toml;
pub fn load_pkg_lockfile(pkg: &Package) -> CargoResult<Option<Resolve>> {
None => {}
}
- try!(try!(File::create(dst)).write_all(out.as_bytes()));
+ try!(paths::write(dst, out.as_bytes()));
Ok(())
}
use std::collections::HashMap;
use std::env;
-use std::fs::{self, File};
+use std::fs;
use std::io::prelude::*;
use std::iter::repeat;
use std::path::{Path, PathBuf};
use ops;
use sources::{RegistrySource};
use util::config;
+use util::paths;
use util::{CargoResult, human, ChainError, ToUrl};
use util::config::{Config, ConfigValue, Location};
use util::important_paths::find_root_manifest_for_cwd;
ref keywords, ref readme, ref repository, ref license, ref license_file,
} = *manifest.metadata();
let readme = match *readme {
- Some(ref readme) => {
- let path = pkg.root().join(readme);
- let mut contents = String::new();
- try!(File::open(&path).and_then(|mut f| {
- f.read_to_string(&mut contents)
- }).chain_error(|| {
- human("failed to read the specified README")
- }));
- Some(contents)
- }
+ Some(ref readme) => Some(try!(paths::read(&pkg.root().join(readme)))),
None => None,
};
match *license_file {
use core::dependency::{Dependency, DependencyInner, Kind};
use sources::{PathSource, git};
use util::{CargoResult, Config, internal, ChainError, ToUrl, human};
-use util::{hex, Sha256};
+use util::{hex, Sha256, paths};
use ops;
static DEFAULT: &'static str = "https://github.com/rust-lang/crates.io-index";
///
/// This requires that the index has been at least checked out.
pub fn config(&self) -> CargoResult<RegistryConfig> {
- let mut f = try!(File::open(&self.checkout_path.join("config.json")));
- let mut contents = String::new();
- try!(f.read_to_string(&mut contents));
+ let contents = try!(paths::read(&self.checkout_path.join("config.json")));
let config = try!(json::decode(&contents));
Ok(config)
}
pkg)))
}
- try!(try!(File::create(&dst)).write_all(resp.get_body()));
+ try!(paths::write(&dst, resp.get_body()));
Ok(dst)
}
use rustc_serialize::{Encodable,Encoder};
use toml;
use core::{MultiShell, Package};
-use util::{CargoResult, ChainError, Rustc, internal, human};
+use util::{CargoResult, ChainError, Rustc, internal, human, paths};
use util::toml as cargo_toml;
Location::Project => unimplemented!(),
};
try!(fs::create_dir_all(file.parent().unwrap()));
- let mut contents = String::new();
- let _ = File::open(&file).and_then(|mut f| f.read_to_string(&mut contents));
+ let contents = paths::read(&file).unwrap_or(String::new());
let mut toml = try!(cargo_toml::parse(&contents, &file));
toml.insert(key.to_string(), value.into_toml());
- let mut out = try!(File::create(&file));
- try!(out.write_all(toml::Value::Table(toml).to_string().as_bytes()));
+
+ let contents = toml::Value::Table(toml).to_string();
+ try!(paths::write(&file, contents.as_bytes()));
Ok(())
}
use std::env;
use std::ffi::{OsStr, OsString};
+use std::fs::File;
+use std::io::prelude::*;
use std::path::{Path, PathBuf, Component};
use util::{human, internal, CargoResult, ChainError};
Some(y) => match a.next() {
Some(x) if x == y => continue,
_ => return None,
- },
+ },
None => return Some(a.as_path()),
}
}
}
+pub fn read(path: &Path) -> CargoResult<String> {
+ (|| -> CargoResult<String> {
+ let mut ret = String::new();
+ let mut f = try!(File::open(path));
+ try!(f.read_to_string(&mut ret));
+ Ok(ret)
+ }).chain_error(|| {
+ internal(format!("failed to read `{}`", path.display()))
+ })
+}
+
+pub fn write(path: &Path, contents: &[u8]) -> CargoResult<()> {
+ (|| -> CargoResult<()> {
+ let mut f = try!(File::create(path));
+ try!(f.write_all(contents));
+ Ok(())
+ }).chain_error(|| {
+ internal(format!("failed to write `{}`", path.display()))
+ })
+}
+
#[cfg(unix)]
pub fn path2bytes(path: &Path) -> CargoResult<&[u8]> {
use std::os::unix::prelude::*;